package com.kpmg.datalake.vbaapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.kpmg.datalake.accountcheck.vo.AccountQueryVO;
import com.kpmg.datalake.common.controller.BaseController;
import com.kpmg.datalake.common.model.DimInfo;
import com.kpmg.datalake.common.model.OptionDataModel;
import com.kpmg.datalake.common.model.RbacFunction;
import com.kpmg.datalake.common.privilege.ValidateDataAuth;
import com.kpmg.datalake.common.service.IPrjRptSqlMpngService;
import com.kpmg.datalake.common.service.RbacFunctionService;
import com.kpmg.datalake.common.utils.AssertUtil;
import com.kpmg.datalake.common.utils.ExceptionUtil;
import com.kpmg.datalake.common.vo.ServerResponse;
import com.kpmg.datalake.db.model.SysUsrOption;
import com.kpmg.datalake.operatelog.annotation.OptLog;
import com.kpmg.datalake.operatelog.enums.LogTypeEnum;
import com.kpmg.datalake.vbaapi.service.VbaVisualService;
import com.kpmg.datalake.vbaapi.vo.VbaParamsDTO;
import com.kpmg.datalake.vbaapi.vo.VbaReportOptionVO;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Description: VBA可视化功能接口
 *
 * @author : timqian
 * @author alex [alex.zhao@kpmg.com] create : 2018-05-17
 */
@RestController
@RequestMapping("/vbaVisual")
public class VbaVisualController extends BaseController {

  private static final Logger logger = LoggerFactory.getLogger(VbaVisualController.class);


  @Resource
  private VbaVisualService vbaVisualService;

  @Resource
  private RbacFunctionService rbacFunctionService;

  @Resource
  private IPrjRptSqlMpngService prjRptSqlMpngService;

  /**
   * 返回用户有权限的菜单集合 基于VBA可视化,根据用户提供树形菜单,第一层为项目Project,二层为"报表查询"/"基础数据"/"Package",三层为具体报表菜单
   *
   * @return ServerResponse
   */
  @PostMapping(value = "/userMenu")
  public ServerResponse<ArrayList<RbacFunction>> getUserMenu() {
    return rbacFunctionService.vbaTreeJsonObject();
  }

  /**
   * 获取默认报表的客户菜单选项
   *
   * @param vbaParamsDTO 查询参数
   * @return ServerResponse
   */
  @PostMapping(value = "/userDefaultReportCorp")
  public ServerResponse<VbaReportOptionVO<DimInfo>> getDefaultReportCorp(
      @RequestBody VbaParamsDTO vbaParamsDTO) {
    return vbaVisualService.initCorp(vbaParamsDTO);

  }

  /**
   * 获取默认报表开始结束日期选项
   *
   * @param vbaParamsDTO 参数, 用于模糊查询
   * @return ServerResponse
   */
  @PostMapping(value = "/userDefaultReportDates")
  public ServerResponse<ArrayList<VbaReportOptionVO<JSONObject>>> getDefaultReportDates(
      @RequestBody VbaParamsDTO vbaParamsDTO) {

    return vbaVisualService.initReportDate(vbaParamsDTO);

  }

  /**
   * 获取默认报表-部门选项
   *
   * @param vbaParamsDTO 参数, 用于模糊查询
   * @return ServerResponse<VbaReportOptionVO < DimInfo>>
   */
//  @PostMapping(value = "/userDefaultReportDept")
//  public ServerResponse<VbaReportOptionVO<DimInfo>> getDefaultReportDept(
//      @RequestBody VbaParamsDTO vbaParamsDTO) {
//    return vbaVisualService.initDept(vbaParamsDTO);
//
//
//  }

  /**
   * 获取默认报表-交易对手选项
   *
   * @param vbaParamsDTO 参数, 用于模糊查询
   * @return ServerResponse<VbaReportOptionVO < DimInfo>>
   */
//  @PostMapping(value = "/userDefaultReportCst")
//  public ServerResponse<VbaReportOptionVO<DimInfo>> getDefaultReportCst(
//      @RequestBody VbaParamsDTO vbaParamsDTO) {
//    return vbaVisualService.initCst(vbaParamsDTO);
//
//  }

  /**
   * 获取默认报表-组团选项
   *
   * @param vbaParamsDTO 参数, 用于模糊查询
   * @return ServerResponse<VbaReportOptionVO < DimInfo>>
   */
//  @PostMapping(value = "/userDefaultReportPrj")
//  public ServerResponse<VbaReportOptionVO<DimInfo>> getDefaultReportPrj(
//      @RequestBody VbaParamsDTO vbaParamsDTO) {
//    return vbaVisualService.initPrj(vbaParamsDTO);
//  }

  /**
   * 获取账套信息
   *
   * @param vbaParamsDTO 参数, 用于模糊查询
   * @return ServerResponse
   */
  @PostMapping(value = "/getAllBookSet")
  public ServerResponse<VbaReportOptionVO<JSONObject>> getAllBookSet(
      @RequestBody VbaParamsDTO vbaParamsDTO) {
    return vbaVisualService.queryAllBookSet(vbaParamsDTO);
  }

  /**
   * 获取科目余额列表信息
   *
   * @param vbaParamsDTO 参数, 用于模糊查询
   * @return ServerResponse
   */
  @PostMapping(value = "/getSubBalanceAccount")
  @OptLog(logType = LogTypeEnum.OPERATION, module = "vbaApi", description = "查询科目余额表数据")
  @ValidateDataAuth(key = "#vbaParamsDTO.getProjectId()")
  public ServerResponse<ArrayList<Map<String, Object>>> getSubBalanceAccount(
      @RequestBody VbaParamsDTO vbaParamsDTO) {
    ServerResponse<ArrayList<Map<String, Object>>> result;
    try {
      result = vbaVisualService.querySubBalanceAccount(vbaParamsDTO);
      AssertUtil.assertTrue(result.isSuccess(), result.getMsg());
    } catch (Exception e) {
      return ExceptionUtil.getExceptionMsg(e);
    }
    return result;
  }

  /**
   * 根据选项,获取默认报告的数据,参数后期需要被转化为VO直接传入
   *
   * @param reportCondition reportCondition
   * @return ServerResponse
   */
//  @PostMapping(value = "/defaultReport")
//  public ServerResponse<JSONObject> getDefaultReport(@RequestBody ReportCondition reportCondition) {
//    SysUsr user = ContextHolderUtils.getLoginUser();
//    String username = EncrypUtil.encryptAes(user.getUsrAcc(), Constants.ENCRY_KEY);
//    logger.info("用户:{}获取默认报告的数据", username);
//    if (StringUtils.isAllBlank(reportCondition.getDeptNm())) {
//      reportCondition.setDeptNm("部门合计");
//    }
//
//    if (StringUtils.isAllBlank(reportCondition.getCstNm())) {
//      reportCondition.setCstNm("交易对手合计");
//    }
//    if (StringUtils.isAllBlank(reportCondition.getPrjNm())) {
//      reportCondition.setPrjNm("组团合计");
//    }
//
//    JSONObject json;
//    try {
//      json = new JSONObject();
//      json.put(Constants.HEAD,
//          prjRptSqlMpngService.selectGeneralHeader(reportCondition.getProjectId(),
//              VbaProjectTypeEnum.DEFAULT_REPORT.getCode(),
//              ContextHolderUtils.getLocale().toString()));
//      ServerResponse<ArrayList<Map<String, Object>>> result =
//          vbaVisualService.queryDefaultReport(reportCondition);
//      AssertUtil.assertTrue(result.isSuccess(), result.getMsg());
//      json.put(Constants.DATA, result.getData());
//    } catch (Exception e) {
//      return ExceptionUtil.getExceptionMsg(e);
//    }
//    return super.doSuccess(json);
//  }

  /**
   * 获取序时帐报表
   *
   * @return ServerResponse
   */
  @PostMapping(value = "/getTimeAccountResult")
  @OptLog(logType = LogTypeEnum.OPERATION, module = "vbaApi", description = "查询序时帐数据")
  @ValidateDataAuth(key = "#vbaParamsDTO.getProjectId()")
  public ServerResponse<ArrayList<Map<String, Object>>> getTimeAccount(
      @RequestBody VbaParamsDTO vbaParamsDTO) {
    ServerResponse<ArrayList<Map<String, Object>>> result;
    try {
      result = vbaVisualService.getTimeAccount(vbaParamsDTO);
      AssertUtil.assertTrue(result.isSuccess(), result.getMsg());
    } catch (Exception e) {
      return ExceptionUtil.getExceptionMsg(e);
    }
    return result;
  }

  /**
   * @param vbaParamsDTO 参数
   * @return ServerResponse
   */
  @PostMapping(value = "/getHistoryReportCondition")
  public ServerResponse<HashMap<String, Object>> getHistoryReportCondition(
      @RequestBody VbaParamsDTO vbaParamsDTO) {
    return vbaVisualService.getReportDefaultSelections(vbaParamsDTO.getProjectId(),
        vbaParamsDTO.getReportId());

  }

  /**
   * @param optionData 参数
   * @return ServerResponse
   */
  @PostMapping(value = "/saveOptionData")
  public ServerResponse<String> saveOptionData(@RequestBody OptionDataModel optionData) {
    return vbaVisualService.saveOptionData(optionData);
  }

  /**
   * @param vbaParamsDTO 参数
   * @return ServerResponse
   */
  @PostMapping(value = "/queryDefaultOptionData")
  public ServerResponse<ArrayList<SysUsrOption>> queryDefaultOptionData(
      @RequestBody VbaParamsDTO vbaParamsDTO) {
    return vbaVisualService.queryDefaultOptionData(vbaParamsDTO);
  }

  /**
   * 根据选项,获取默认报告的数据,参数后期需要被转化为VO直接传入 下钻
   * 
   * @param condition condition
   * @return ServerResponse
   */
//  @PostMapping(value = "/vbaSearchReport")
//  @OptLog(logType = LogTypeEnum.OPERATION, module = "vbaApi", description = "校验报告")
//  public ServerResponse<JSONObject> getSearchReport(@RequestBody ReportCondition condition) {
//    JSONObject json;
//    try {
//      json = new JSONObject();
//      json.put(Constants.HEAD, prjRptSqlMpngService.selectGeneralHeader(condition.getProjectId(),
//          VbaProjectTypeEnum.DEFAULT_REPORT.getCode(), ContextHolderUtils.getLocale().toString()));
//      ServerResponse<ArrayList<Map<String, Object>>> result =
//          vbaVisualService.searchReport(condition);
//      AssertUtil.assertTrue(result.isSuccess(), result.getMsg());
//      json.put(Constants.DATA, result.getData());
//    } catch (Exception e) {
//      return ExceptionUtil.getExceptionMsg(e);
//    }
//    return super.doSuccess(json);
//
//  }


  /**
   * 根据用户名/项目Id和对应报告的参数,获得内部对账数据
   *
   * @param accountQueryVO accountQueryVO
   * @return ServerResponse
   */
  @PostMapping(value = "/getChkData")
  @ValidateDataAuth(key = "#accountQueryVO.getProjectId()")
  public ServerResponse<JSONObject> getChkData(@RequestBody AccountQueryVO accountQueryVO) {
    ServerResponse<JSONObject> result;
    try {
      result = vbaVisualService.getChkAccountData(accountQueryVO, -1);
      AssertUtil.assertTrue(result.isSuccess(), result.getMsg());
    } catch (Exception e) {
      return ExceptionUtil.getExceptionMsg(e);
    }
    return result;
  }
}
